package pkg

import (
	"context"
	"github.com/cheng1005/XxCh-Common/common/global"
	"log"
	"strings"

	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)

func Upload(filename string, dst string) string {

	aliConfig := global.Nacos.Aliyun
	// 加载默认配置并设置凭证提供者和区域
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewStaticCredentialsProvider(aliConfig.AccessKeyID, aliConfig.AccessKeySecret)).
		WithRegion("cn-shanghai")

	// 创建OSS客户端
	client := oss.NewClient(cfg)

	// 创建上传对象的请求
	request := &oss.PutObjectRequest{
		Bucket: oss.Ptr(aliConfig.Bucket), // 存储空间名称
		Key:    oss.Ptr(filename),         // 对象名称
		Body:   strings.NewReader(dst),    // 要上传的内容
	}

	// 执行上传对象的请求
	result, err := client.PutObject(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to put object %v", err)
	}
	// 打印上传对象的结果
	log.Printf("put object result:%#v\n", result)
	return aliConfig.URL + filename
}
